package com.ikas.ai.enums;

import com.ikas.ai.utils.DateTimeUtil;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description: 数据源列类型
 * @Author: qi.yilai
 * @Date: 2022/11/3 16:22
 * @Version: 1.0.0
 **/
public enum DataTypeEnum {
    /**
     *
     */
    INT("int") {
        @Override
        public boolean valid(String value) {
            try {
                Integer.parseInt(value);
            } catch (Exception exception) {
                return false;
            }
            return true;
        }

        @Override
        public Object convert(String value) {
            return Integer.parseInt(value);
        }

        @Override
        public DateTimePatternEnum findPattern(String value) {
            return null;
        }
    },

    BIGINT("bigint") {
        @Override
        public boolean valid(String value) {
            try {
                Long.parseLong(value);
            } catch (Exception exception) {
                return false;
            }
            return true;
        }

        @Override
        public Object convert(String value) {
            return Long.parseLong(value);
        }

        @Override
        public DateTimePatternEnum findPattern(String value) {
            return null;
        }
    },

    DOUBLE("double") {
        @Override
        public boolean valid(String value) {
            try {
                Double.parseDouble(value);
            } catch (Exception exception) {
                return false;
            }
            return true;
        }

        @Override
        public Object convert(String value) {
            return Double.parseDouble(value);
        }

        @Override
        public DateTimePatternEnum findPattern(String value) {
            return null;
        }
    },
    FLOAT("float") {
        @Override
        public boolean valid(String value) {
            try {
                Float.parseFloat(value);
            } catch (Exception exception) {
                return false;
            }
            return true;
        }

        @Override
        public Object convert(String value) {
            return Float.parseFloat(value);
        }

        @Override
        public DateTimePatternEnum findPattern(String value) {
            return null;
        }
    },
    BOOLEAN("boolean") {
        @Override
        public boolean valid(String value) {
            if (StringUtils.isEmpty(value)) {
                return false;
            }
            return "true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value);
        }

        @Override
        public Object convert(String value) {
            return Boolean.parseBoolean(value);
        }

        @Override
        public DateTimePatternEnum findPattern(String value) {
            return null;
        }
    },


    TIMESTAMP("timestamp") {
        @Override
        public boolean valid(String value) {
            DateTimePatternEnum[] patterns = DateTimePatternEnum.values();
            for (DateTimePatternEnum dateTimePatternEnum : patterns) {
                try {
                    DateTimeUtil.getTimestamp(value, dateTimePatternEnum.getPattern());
                } catch (Exception exception) {
                    continue;
                }
                return true;
            }
            return false;
        }

        @Override
        public Object convert(String value) {
            DateTimePatternEnum[] patterns = DateTimePatternEnum.values();
            for (DateTimePatternEnum dateTimePatternEnum : patterns) {
                try {
                    return DateTimeUtil.getTimestamp(value, dateTimePatternEnum.getPattern());
                } catch (Exception exception) {
                    continue;
                }
            }
            return null;
        }

        @Override
        public DateTimePatternEnum findPattern(String value) {
            DateTimePatternEnum[] patterns = DateTimePatternEnum.values();
            for (DateTimePatternEnum dateTimePatternEnum : patterns) {
                try {
                    DateTimeUtil.getTimestamp(value, dateTimePatternEnum.getPattern());
                } catch (Exception exception) {
                    continue;
                }
                return dateTimePatternEnum;
            }
            return null;
        }
    },
    DATE("date") {
        @Override
        public boolean valid(String value) {
            DatePatternEnum[] patterns = DatePatternEnum.values();
            for (DatePatternEnum datePatternEnum : patterns) {
                try {
                    DateTimeUtil.getDateCore(value, datePatternEnum.getPattern());
                } catch (Exception exception) {
                    continue;
                }
                return true;
            }
            return false;
        }

        @Override
        public Object convert(String value) {
            DatePatternEnum[] patterns = DatePatternEnum.values();
            for (DatePatternEnum datePatternEnum : patterns) {
                try {
                    return DateTimeUtil.getDateCore(value, datePatternEnum.getPattern());
                } catch (Exception exception) {
                    continue;
                }
            }
            return null;
        }

        @Override
        public DatePatternEnum findPattern(String value) {
            DatePatternEnum[] patterns = DatePatternEnum.values();
            for (DatePatternEnum datePatternEnum : patterns) {
                try {
                    DateTimeUtil.getDateCore(value, datePatternEnum.getPattern());
                } catch (Exception exception) {
                    continue;
                }
                return datePatternEnum;
            }
            return null;
        }
    },


    STRING("string") {
        @Override
        public boolean valid(String value) {
            return true;
        }

        @Override
        public Object convert(String value) {
            return value;
        }

        @Override
        public DateTimePatternEnum findPattern(String value) {
            return null;
        }
    },

    NONE("none") {
        @Override
        public boolean valid(String value) {
            return true;
        }

        @Override
        public Object convert(String value) {
            return null;
        }

        @Override
        public Pattern findPattern(String value) {
            return null;
        }
    };

    private String code;

    public abstract boolean valid(String value);

    public abstract Object convert(String value);

    public abstract Pattern findPattern(String value);

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    DataTypeEnum(String code) {
        this.code = code;
    }

    /**
     * 根据字符串解析出对应的类型
     *
     * @param value
     * @return
     */
    public static DataTypeEnum parseType(String value) {
        DataTypeEnum[] dataTypeEnums = DataTypeEnum.values();
        for (DataTypeEnum dt : dataTypeEnums) {
            if (dt.valid(value)) {
                return dt;
            }
        }
        return null;
    }

    /**
     * 根据字符串解析出对应的类型
     *
     * @param value
     * @return
     */
    public static String parseTypeCode(String value) {
        DataTypeEnum[] dataTypeEnums = DataTypeEnum.values();
        for (DataTypeEnum dt : dataTypeEnums) {
            if (dt.valid(value)) {
                return dt.getCode();
            }
        }
        return null;
    }
}
